package com.axzhengxin.platform.sync.impl;

import cn.hutool.db.Db;
import com.axzhengxin.platform.business.domain.BizFlowTask;
import com.axzhengxin.platform.business.mapper.BizFlowTaskMapper;
import com.axzhengxin.platform.sync.ISyncDataService;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.sql.DataSource;

/**
 * ApproveBizFlowTaskSyncServiceImpl
 *
 * @author jiangnan
 * @date 2021/4/6
 */
@Service
public class ApproveBizFlowTaskSyncServiceImpl implements ISyncDataService {

    @Autowired
    private DataSource ecscDs;

    @Autowired
    private BizFlowTaskMapper flowTaskMapper;

    @SneakyThrows
    @Override
    public void exec() {
        Db.use(ecscDs)
                .query("select t.flow_uid, t.current_node_user_id as user_id, t.current_node_user_id as next_user_id, t.current_node_user_id as create_by, t.biz_type, t.status,t.create_time from biz_flow_info t where t.biz_type = 'APPROVE'", BizFlowTask.class)
                .stream()
                .peek(flowTask -> {
                    if (2000 == flowTask.getStatus() || 3000 == flowTask.getStatus()) {
                        flowTask.setLastFlag(1);
                    } else {
                        flowTask.setLastFlag(0);
                    }
                })
                .forEach(flowTaskMapper::insertBizFlowTask);

    }

    @Override
    public int getOrder() {
        return BizApproveInfoSyncServiceImpl.ORDER + 2;
    }
}
